package com.ficus.road.maintain.third.zh.xlj;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.xlj.Xlj;
import com.ficus.road.maintain.core.model.zh.xlj.XljBasicInfoZH;
import com.ficus.road.maintain.core.model.zh.xlj.XljLatestInfo;
import com.ficus.road.maintain.core.model.zh.xlj.XljLatestInfoZH;
import com.ficus.road.maintain.dao.xlj.XljDao;
import com.ficus.road.maintain.dao.xlj.XljWarningDao;
import com.ficus.road.maintain.dao.xlj.XljWaterLevelHistoryDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import com.google.common.base.Objects;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/6/27 下午4:51
 */
@Component
public class ZHXljLatest extends BaseZH {
    @Value("${present.flag}")
    private Boolean present;
    @Autowired
    private XljDao xljDao;
    @Autowired
    private XljWarningDao xljWarningDao;
    @Autowired
    private XljWaterLevelHistoryDao xljWaterLevelHistoryDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangXljLatest";
        arg1 = "";
        errMsg = "调用中海接口获取下立交实时数据异常!";
        successMsg = "调用中海接口获取下立交实时数据成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "${xlj.sync_cron}", zone = "GMT+8:00")
    public void refreshXljLatest() {
        String xljLatestInfo = getZHData();
        if (StringUtils.isEmpty(xljLatestInfo)) {
            return;
        }
        Date date = new Date();
        List<XljLatestInfoZH> xljLatestInfoZHList = JSONArray.parseArray(xljLatestInfo, XljLatestInfoZH.class);
        List<XljLatestInfo> xljLatestInfoList = xljLatestInfoZHList.stream().map(item -> item.convert2XljLatestInfo(item, date)).collect(Collectors.toList());
        List<Xlj> xljList = xljDao.findAll();
        xljLatestInfoList.forEach(item -> {
            Optional<Xlj> any = xljList.stream().filter(xlj -> Objects.equal(xlj.getUniIdHuwan(), item.getXljId())).findAny();
            if (any.isEmpty()) {
                return;
            }
            Xlj xlj = any.get();
            xlj.setXljLatestInfo(item);
            List<XljBasicInfoZH.Camera> cameraList = item.getCameraList();
            if (ObjectUtils.isNotEmpty(xlj.getCameras())) {
                xlj.getCameras().forEach(camera -> {
                    Optional<XljBasicInfoZH.Camera> cameraOptional = cameraList.stream().filter(c -> c.getCameraId().equals(camera.getCameraId())).findAny();
                    if (cameraOptional.isEmpty()) {
                        return;
                    }
                    XljBasicInfoZH.Camera xljBasicCamera = cameraOptional.get();
                    camera.setCameraStatus(xljBasicCamera.getCameraStatus());
                    camera.setUpdateTime(xljBasicCamera.getUpdateTime());
                });
            }
            xljDao.saveOrUpdate(xlj);
        });
    }

    @Override
    public boolean printLogInfo() {
        return false;
    }
}
